//
//  LCDMultiFunctionSegmentedView.m
//  BSSLLoanProcessPod
//
//  Created by 传铎 on 2020/6/29.
//  Copyright © 2020 PURANG. All rights reserved.
//

#import "LCDMultiFunctionSegmentedView.h"
#import "ZQNewUnityObject.h"
@implementation LCDMultiFunctionSegmentedView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}
- (id)initWithOriginY:(CGRect)Frame left:(NSInteger)left right:(NSInteger)right headTitle:(NSString *)headTitle headTitleFont:(UIFont *)headTitleFont headTitleColor:(NSString *)headTitleColor Titles:(NSArray *)titles titleFont:(UIFont *)titleFont titleColor:(NSString *)titleColor titleSelectedColor:(NSString *)titleSelectedColor isUniform:(BOOL)isUniform  maxUniformCount:(NSInteger)maxUniformCount isVerticalLine:(BOOL)isVerticalLine verticallineColor:(NSString *)verticallineColor verticallineHeight:(float )verticallineHeight isUnderline:(BOOL)isUnderline underlineColor:(NSString *)underlineColor underlineWidth:(float )underlineWidth underlineHeight:(float )underlineHeight{
    if (self = [super initWithFrame:Frame]) {
        underlineWidth = 71.5;
        underlineHeight = 43;
        _widthArr = [NSMutableArray array];
        _contentOffsetWidth = 0;
        CGFloat count = left;
        CGFloat firstWidth = 0.0;
        if (!IsEmpty(headTitle)) {
            
            CGFloat titleWidth;
            
            //            if (titles.count > 4) {
            titleWidth    = [ZQNewUnityObject sizeWithString:headTitle font:headTitleFont width:1000].width;
            
            UILabel * headTitleLable    = [[UILabel alloc] initWithFrame:CGRectMake(left, self.height / 4, titleWidth + 5, self.height / 2)];
            headTitleLable.text = headTitle;
            headTitleLable.font = headTitleFont;
            headTitleLable.textColor = colorCode(@"headTitleColor");
            [self addSubview:headTitleLable];
            
            UITapGestureRecognizer * headtitleTap    = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headTitleClick)];
            [headTitleLable addGestureRecognizer:headtitleTap];
            count = titleWidth + count + 5;
        }
        // 按钮
        UIScrollView * zcScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(count, 0, SCREEN_WIDTH - count - right, self.height)];
        zcScrollView.tag = 2301;
        zcScrollView.showsVerticalScrollIndicator = NO;
        zcScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:zcScrollView];
        
        CGFloat btnHeight   = self.height / 2;
        
        CGFloat orgintX     = 0;
        for (int i = 0; i < titles.count; i ++) {
            CGFloat btnWidth;
            if (!isUniform) {
                btnWidth    = [ZQNewUnityObject sizeWithString:titles[i] font:titleFont width:1000].width;
                if (i == 0) {
                    firstWidth = btnWidth;
                }
                
            }else if (titles.count < maxUniformCount) {
                if (isVerticalLine) {
                    btnWidth = (SCREEN_WIDTH - count - right - (titles.count - 1) * 20)/titles.count;
                    firstWidth = btnWidth;
                }else{
                    btnWidth = (SCREEN_WIDTH - count - right - (titles.count - 1) * 10)/titles.count;
                    firstWidth = btnWidth;
                }
            }else{
                if (isVerticalLine) {
                    btnWidth = (SCREEN_WIDTH - count - right - (maxUniformCount - 1) * 20)/maxUniformCount;
                    firstWidth = btnWidth;
                }else{
                    btnWidth = (SCREEN_WIDTH - count - right - (maxUniformCount - 1) * 10)/maxUniformCount;
                    firstWidth = btnWidth;
                }
            }
            
            UIButton * functionBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
            functionBtn.frame       = CGRectMake(orgintX, self.height / 4, btnWidth, btnHeight);
            functionBtn.layer.cornerRadius = 3;
            [functionBtn setTitle:titles[i] forState:0];
            [functionBtn setTitleColor:colorCode(titleColor) forState:0];
            [functionBtn setTitleColor:colorCode(titleSelectedColor) forState:UIControlStateSelected];
            functionBtn.titleLabel.font = titleFont;
            functionBtn.tag = 60000 + i;
            [functionBtn addTarget:self action:@selector(datafunctionClick:) forControlEvents:UIControlEventTouchUpInside];
            [zcScrollView addSubview:functionBtn];
            
            orgintX += btnWidth;
            orgintX += 10;
            if (i == 0) {
                functionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            }
            // 竖线
            if (i != titles.count - 1 && isVerticalLine) {
                UIView * suline     = [[UIView alloc] initWithFrame:CGRectMake(orgintX, (self.height - verticallineHeight)/2, 1, verticallineHeight)];
                suline.backgroundColor  = colorCode(verticallineColor);
                [zcScrollView addSubview:suline];
                
                orgintX += 10;
            }
            [_widthArr addObject:[NSString stringWithFormat:@"%.2f",orgintX]];
        }
        
        if (isUnderline) {
            UIImageView *lineView = [[UIImageView alloc]initWithFrame:CGRectMake((firstWidth - underlineWidth) / 2 , 10, underlineWidth, underlineHeight)];
            lineView.image = imageNameStr(@"pic_chosse");
            lineView.tag = 2300;
            _widthC = (firstWidth - underlineWidth) / 2;
            [zcScrollView addSubview:lineView];
//            lineView.backgroundColor = colorCode(underlineColor);
        }
        _isVerticalLines = isVerticalLine;
        _lineWidth = underlineWidth;
        _lineHeight = underlineHeight;
        zcScrollView.contentSize    = CGSizeMake(orgintX, self.height);
    }
    return self;
}

- (void)datafunctionClick:(UIButton *)sender{
    sender.selected = YES;
    NSInteger tagCount = 0;
    for (NSString *string in _widthArr) {
        Format(string);
        UIButton *subBtn = [self viewWithTag:tagCount + 60000];
        if (subBtn != sender) {
            subBtn.selected = NO;
            subBtn.titleLabel.font = fontCount(16);
        }else{
            subBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        }
        tagCount++;
    }
    UIImageView *diView = [self viewWithTag:2300];
    WEAKSELF;
    UIScrollView *scrollView = [self viewWithTag:2301];
    CGPoint pt = CGPointZero;
    BOOL canScrolle = NO;
    if (_widthArr.count > 4 ) {
        if ([[_widthArr lastObject]floatValue] > scrollView.frame.size.width) {
            if ([_widthArr[sender.tag - 60000]floatValue] >= scrollView.frame.size.width) {
                if (sender.tag > 60003 && _widthArr.count > sender.tag - 60000 + 3) {
                    if ([_widthArr[sender.tag - 60000 - 2]floatValue] < [[_widthArr lastObject]floatValue] - scrollView.frame.size.width) {
                        pt.x = [_widthArr[sender.tag - 60000 + 3]floatValue] - [_widthArr[[self getScrollWidthCount]]floatValue];
                        canScrolle = YES;
                    }else{
                        pt.x = [[_widthArr lastObject]floatValue] - scrollView.frame.size.width;
                        canScrolle = YES;
                    }
                }else{
                    pt.x = [[_widthArr lastObject]floatValue] - scrollView.frame.size.width;
                    canScrolle = YES;
                }
            }else{
                pt.x = 0;
                canScrolle = YES;
            }
        }
    }else{
        canScrolle = YES;
    }
    
    NSInteger counts = 0;
    if (_isVerticalLines) {
        counts = 20;
    }else{
        counts = 10;
    }
    if (canScrolle) {
        
        WEAKSELF;
        [UIView animateWithDuration:0.3 animations:^{
            if (weakSelf.widthArr.count > 4) {
                scrollView.contentOffset = pt;
            }
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                if (sender.tag > 60000) {
                    
                    float count = [weakSelf.widthArr[sender.tag - 60000 - 1]floatValue];
                    diView.frame = CGRectMake(count + ([weakSelf.widthArr[sender.tag - 60000]floatValue] - [weakSelf.widthArr[sender.tag - 60000 - 1]floatValue] - weakSelf.lineWidth - counts) / 2, 10, weakSelf.lineWidth, weakSelf.lineHeight);
                }else{
                    
                    diView.frame = CGRectMake(weakSelf.widthC, 10, weakSelf.lineWidth, weakSelf.lineHeight);
                }
            }];
        }];
        
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            if (sender.tag > 60000) {
                
                float count = [weakSelf.widthArr[sender.tag - 60000 - 1]floatValue];
                diView.frame = CGRectMake(count + ([weakSelf.widthArr[sender.tag - 60000]floatValue] - [weakSelf.widthArr[sender.tag - 60000 - 1]floatValue] - weakSelf.lineWidth  - counts) / 2 , 10, weakSelf.lineWidth, weakSelf.lineHeight);
            }else{
                
                diView.frame = CGRectMake(weakSelf.widthC,10, weakSelf.lineWidth, weakSelf.lineHeight);
            }
        }];
    }
    
    _segmentClock(sender.tag - 60000);
    
}
- (NSInteger)getScrollWidthCount{
    UIScrollView *scrollView = [self viewWithTag:2301];
    NSInteger count = 0;
    for (int i = 0; i < _widthArr.count; i ++) {
        if ([_widthArr[i]floatValue] > scrollView.frame.size.width) {
            count = i;
            break;
        }
    }
    return count;
}
- (void)changeSegmentedControlWithIndex:(NSInteger)index{
    NSInteger tagCount = 0;
    for (NSString *string in _widthArr) {
        Format(string);
        UIButton *subBtn = [self viewWithTag:tagCount + 60000];
        if (subBtn.tag != index + 60000) {
            subBtn.selected = NO;
            subBtn.titleLabel.font = fontCount(16);
        }else{
            subBtn.selected = YES;
            subBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        }
        
        tagCount++;
    }
    UIImageView *diView = [self viewWithTag:2300];
    WEAKSELF;
    UIScrollView *scrollView = [self viewWithTag:2301];
    CGPoint pt = CGPointZero;
    BOOL canScrolle = NO;
    if ([[_widthArr lastObject]floatValue] > scrollView.frame.size.width) {
        if ([_widthArr[index + 60000 - 60000]floatValue] >= scrollView.frame.size.width) {
            if (index + 60000 > 60003 && _widthArr.count > index + 60000 - 60000 + 3) {
                if ([_widthArr[index + 60000 - 60000 - 2]floatValue] < [[_widthArr lastObject]floatValue] - scrollView.frame.size.width) {
                    pt.x = [_widthArr[index + 60000 - 60000 + 3]floatValue] - [_widthArr[[self getScrollWidthCount]]floatValue];
                    canScrolle = YES;
                }else{
                    pt.x = [[_widthArr lastObject]floatValue] - scrollView.frame.size.width;
                    canScrolle = YES;
                }
            }else{
                pt.x = [[_widthArr lastObject]floatValue] - scrollView.frame.size.width;
                canScrolle = YES;
            }
        }else{
            pt.x = 0;
            canScrolle = YES;
        }
    }
    NSInteger counts = 0;
    if (_isVerticalLines) {
        counts = 20;
    }else{
        counts = 10;
    }
    if (canScrolle) {
        
        
        [UIView animateWithDuration:0.3 animations:^{
            if (weakSelf.widthArr.count > 4) {
                scrollView.contentOffset = pt;
            }
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                if (index + 60000 > 60000) {
                    
                    float count = [weakSelf.widthArr[index + 60000 - 60000 - 1]floatValue];
                    diView.frame = CGRectMake(count + ([weakSelf.widthArr[index + 60000 - 60000]floatValue] - [weakSelf.widthArr[index + 60000 - 60000 - 1]floatValue] - weakSelf.lineWidth - counts) / 2,10, weakSelf.lineWidth, weakSelf.lineHeight);
                }else{
                    
                    diView.frame = CGRectMake(weakSelf.widthC, 10, weakSelf.lineWidth, weakSelf.lineHeight);
                }
            }];
        }];
        
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            if (index + 60000 > 60000) {
                
                float count = [weakSelf.widthArr[index + 60000 - 60000 - 1]floatValue];
                diView.frame = CGRectMake(count + ([weakSelf.widthArr[index + 60000 - 60000]floatValue] - [weakSelf.widthArr[index + 60000 - 60000 - 1]floatValue] - weakSelf.lineWidth  - counts) / 2 , 10, weakSelf.lineWidth, weakSelf.lineHeight);
            }else{
                
                diView.frame = CGRectMake(weakSelf.widthC, weakSelf.height - weakSelf.lineHeight - 10, weakSelf.lineWidth, weakSelf.lineHeight);
            }
        }];
    }
    
}
- (void)headTitleClick {
    
}
@end
